package com.yer.note.activity;

import static com.yer.note.entity.User.user;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.yer.note.R;
import com.yer.note.databinding.ActivityNoteBinding;
import com.yer.note.entity.User;
import com.yer.note.utils.BaseUtils;

import org.w3c.dom.Text;

import de.hdodenhof.circleimageview.CircleImageView;

public class NoteActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityNoteBinding binding;
    private TextView headUsername;
    private TextView headMotto;
    private CircleImageView imageView;
    public void initView(){
        NavigationView navigationView = findViewById(R.id.nav_view);
        View headerLayout = navigationView.getHeaderView(0);
        headUsername = headerLayout.findViewById(R.id.nav_header_username);
        headMotto = headerLayout.findViewById(R.id.nav_header_motto);
        imageView = headerLayout.findViewById(R.id.nav_header_head);
        System.out.println("加载nav——header");
        if(user.getName()!=null){
            headUsername.setText(user.getName());
        }
        if(user.isPic()){
            imageView.setImageURI(user.getUri());
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                startActivityForResult(intent, 2);
                user.setPic(true);
                User.update(user);
            }
        });
        if(user.getMotto()!=null){
            headMotto.setText(user.getMotto());
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            try {
                // 从相册返回的数据
                Log.e(this.getClass().getName(), "Result:" + data.toString());
                if (data != null) {
                    // 得到图片的全路径
                    Uri uri = data.getData();
                    imageView.setImageURI(uri);
                    user.setUri(uri);
                    User.update(user);
                    Log.e(this.getClass().getName(), "Uri:" + String.valueOf(uri));
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoteBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setSupportActionBar(binding.appBarNote.toolbar);
        binding.appBarNote.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(NoteActivity.this,KeepDiaryActivity.class);
                String diaryUUid = "";
                intent.putExtra("diaryUUid",diaryUUid);
                startActivity(intent);
            }
        });
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home,R.id.nav_label,R.id.nav_settings,R.id.nav_about)
                .setOpenableLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_note);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        initView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.note, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_note);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}